package com.woniuxy.mapper;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.woniuxy.provider.GoodsSqlProvider;
import com.work.framework.pojo.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author fengyao
 * @version 1.0
 * @date 2019/9/19 0019 上午 11:37
 */
@Mapper
@Repository
public interface GoodsMapper {
    /**
     * 根据方法查询
     * @param aid 商品id
     * @return 返回一个goods和Auction的结合类
     */
    @Select("select * from auction where valid=1 and aid=#{aid} ")
    Auction selectGoodsAuctionByGid(Integer aid);

    /**
     * 返回
     * @param gid
     * @return
     */
    @Select("select * from auction a inner join goods g on a.gid=g.gid and a.valid=1 inner join picture p on p.gid=g.gid and p.status=0  where g.gid=#{gid}")
    GoodsAuctionDTO selectGoodsAuctionDTO(Integer gid);

    /**
     * 返回
     * @param aid
     * @return
     */
    @Select("select * from auction a inner join goods g on a.gid=g.gid and a.valid=1 inner join picture p on p.gid=g.gid and p.status=0  where a.aid=#{aid}")
    GoodsAuctionDTO selectGoodsAuctionDTOByAid(Integer aid);

    /**
     * 查询补充图片
     * @param gid 商品id
     * @return 返回补充图片列表
     */
    @Select("select url from picture where gid=#{gid} and status=1")
    List<String> selectOtherPicture(Integer gid);

    @Select("select * from auction")
    List<Auction> selectAuction();


    /**
     * 返回收藏商品的用户账号
     * @param gid 商品id
     * @return 返回用户清单
     */
    @Select("select account from user where uid in (select uid from enshrine where gid=#{gid})")
    List<User> selectUsersByGid(Integer gid);

    /**
     *  返回收藏信息
     * @param uid 用户id
     * @param gid 商品id
     * @return 收藏信息
     */
    @Select("select * from enshrine where uid=#{uid} and gid=#{gid}")
    Enshrine findEnshrineByUidAndGid(Integer uid,Integer gid);

    /**
     * 添加收藏
     * @param uid 用户id
     * @param gid 商品id
     * @return 返回改变行数
     */
    @Select("insert into enshrine values(default,#{uid},#{gid},default)")
    void insertEnshrine(Integer uid,Integer gid);

    /**
     * 修改
     * @param uid 用户id
     * @param gid 商品id
     * @return 影响行数
     */
    @Select("update enshrine set flage=1 where uid=#{uid} and gid=#{gid}")
    void updateEnshrine(Integer uid,Integer gid);

    /**
     * 修改
     * @param uid 用户id
     * @param gid 商品id
     * @return 影响行数
     */
    @Select("update enshrine set flage=0 where uid=#{uid} and gid=#{gid}")
    void noEnshrine(Integer uid,Integer gid);

    /**
     * 修改收藏状态，传入1 为加1 ，传入-1为减1
     * @param count
     * @param gid
     * @return
     */
    @Select("update goods set enshrine=enshrine+#{count} where gid=#{gid}")
    void updateGoodsEnshrineCount(Integer count,Integer gid);

    /**
     * 修改当前竞拍
     * @param gid 商品id
     */
    @Update("update auction set valid=0 where gid=#{gid} and valid=1")
    void updateAuctionValid(Integer gid);

    /**
     * 修改状态值
     * @param aid 竞拍id
     * @param status 修改状态值
     */
    @Update("update auction set status=#{status} and valid=0 where aid=#{aid}")
    void updateAuctionStatus(Integer aid,Integer status);

    /**
     * 插入商品
     * @param goods
     */
    @Insert("insert into goods value(default,#{gname},default,#{gdescribe},1,1,2,#{uid},default) ")
    @SelectKey(statement="select LAST_INSERT_ID()", keyProperty="gid", before=false, resultType=Integer.class)
    void insertGoods(Goods goods);

    /**
     * 插入竞拍信息
     * @param auction
     */
    @Insert("insert into auction value(default,#{gid},#{price},#{cash},#{status},#{created},#{start},#{stop},1,1,1,#{upward}) ")
    @SelectKey(statement="select LAST_INSERT_ID()", keyProperty="aid", before=false, resultType=Integer.class)
    void insertAuction(Auction auction);

    /**
     * 插入竞拍信息
     * @param picture
     */
    @Insert("insert into picture value(default,#{gid},#{status},#{url},default) ")
    void insertPicture(Picture picture);

    /**
     * 根据aid获取Uid
     * @param aid
     * @return
     */
    @Select("select g.uid where goods g inner join auction a on g.gid=a.gid and a.valid=1 where a.aid=#{aid}")
    Integer selectUidByAid(Integer aid);
}
